## CFLAGS = `pkg-config --libs --cflags sdl2` --std=c++11

## 直接编译链接命令如下
## g++ main.cpp core.cpp world.cpp draw.cpp rectangle.cpp solid_barrier.cpp torch.cpp -o main `pkg-config --libs --cflags sdl2` --std=c++11

## 制定寻找文件路径src:../headers，用冒号分隔
## vpath %.c src
## vpath %.h src
CC = g++

TARGET=main

CPPDIR=.
## maybe you wanna change *.cpp files path except main.cpp

SOURCE = main.cpp
SOURCE += $(CPPDIR)/core.cpp \
	$(CPPDIR)/draw.cpp \
	$(CPPDIR)/rectangle.cpp \
	$(CPPDIR)/solid_barrier.cpp \
	$(CPPDIR)/torch.cpp \
	$(CPPDIR)/world.cpp
## end sources definition

VPATH=$(CPPDIR)
OBJ=$(join $(addsuffix obj/, $(dir $(SOURCE))), $(notdir $(SOURCE:.cpp=.o)))
DEPENDS=$(join $(addsuffix .dep/, $(dir $(SOURCE))), $(notdir $(SOURCE:.cpp=.d)))
SDL_LIBS=`sdl2-config --libs` -lSDL2_image -lSDL2_ttf
SDL_INCLUDE=`sdl2-config --cflags`

CFLAGS=-Wall -std=c++11 $(SDL_INCLUDE) -Wall -ggdb -I/usr/local/include -I$(CPPDIR)
LDFLAGS=$(SDL_LIBS) -L/usr/local/lib
## -I编译程序按照-I指定的路进去搜索头文件
## -L编译程序按照－L指定的路进去寻找库文件，一般的在-L的后面可以一次用-l指定多个库文件
## -l编译程序到系统默认路进搜索，如果找不到，到当前目录，如果当前目录找不到，则到
## LD_LIBRARY_PATH等环境变量置顶的路进去查找，如果还找不到，那么编译程序提示找不到库

.PHONY: all clean
all: $(TARGET)
	@true

clean:
	@-rm -f $(TARGET) $(OBJ) $(DEPENDS)
	@rm -rf obj .dep

$(TARGET): $(OBJ)
	@echo "--- Linking target $@"
	@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

obj/%.o: %.cpp
	@mkdir -p $(dir $@)
	@echo "--- Compling $<"
	@$(CC) $(CFLAGS) -c $< -o $@

.dep/%.d: %.cpp
	@mkdir -p $(dir $@)
	@echo --- Building dependencies file for $*.o
	@echo $(CFLAGS)
	@$(SHELL) -ec '$(CC) -M $(CFLAGS) $< | sed "s^$*\.o^obj/$*\.o^" > $@'

## Dependency rule for "other" directory
$(CPPDIR)/.dep/%.d: %.cpp
	@mkdir -p $(dir $@)
	@echo "--- Building dependencies file for $*.o"
	@$(CC) -M $(CFLAGS) $< -o $@
#@$(SHELL) -ec '$(CC) -M $(CFLAGS) $< | sed "s^$*\.o^$(CPPDIR)/obj/$*\.o^" > $@'

## Had to escape the dots

## Include the dependency files
-include $(DEPENDS)
## write down here maybe told make make dependencies for all above targets. 
